﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Module.Interfaces;
using Core.Module.Interfaces.Controller;
using Microsoft.Practices.Unity;
using Core.Module.Interfaces.Manager;
using Core.Module.Interfaces.Model;
using umbraco.presentation;

namespace Core.Module.Controller
{
    public class NodeController:INodeController<IFrontend>
    {
        [Dependency]
        public INodeManager<IFrontend> Manager { get; set; }

        public IModuleNode Get(string nodeId)
        {
            int nid;
            if (!int.TryParse(nodeId, out nid) || nid < -1) return null;
            return Manager.Get(nid);
        }
        
        public IModuleNode Get(IModuleNode sourceNode, string sourceProperty)
        {
            string nodeid = Manager.GetPropertyValue(sourceNode, sourceProperty) as string;
            if (string.IsNullOrEmpty(nodeid)) return null;
            return Get(nodeid);
        }


        public IEnumerable<IModuleNode> GetChildren(string parentId)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChildren(nid);
        }

        public IEnumerable<IModuleNode> GetChildren(string parentId, string nodeTypeName)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChildren(nid,nodeTypeName);
        }

        public IModuleNode GetChild(string parentId, string nodeTypeName)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChild(nid, nodeTypeName);
        }


        public object GetPropertyValue(IModuleNode node, string propertyName)
        {
            return Manager.GetPropertyValue(node, propertyName);
        }

        public IModuleNode GetPropertyAsNode(IModuleNode  node, string  propertyName)
        {
            return Manager.GetPropertyAsNode(node, propertyName);
        }


        public IEnumerable<KeyValuePair<string, string>> GetProperties(IModuleNode node)
        {
            if (node == null|| node.Data == null) return null;
            return Manager.GetProperties(node);
        }
    }


    public class NodeControllerBackOffice : INodeController<IBackOffice>
    {
        [Dependency]
        public INodeManager<IBackOffice> Manager{get;set;}

        public IModuleNode Get(string nodeId)
        {
            int nid;
            if (!int.TryParse(nodeId, out nid) || nid < -1) return null;
            return Manager.Get(nid);
        }

        public IModuleNode Get(IModuleNode sourceNode, string sourceProperty)
        {
            string nodeid = Manager.GetPropertyValue(sourceNode, sourceProperty) as string;
            if (string.IsNullOrEmpty(nodeid)) return null;
            return Get(nodeid);
        }


        public IEnumerable<IModuleNode> GetChildren(string parentId)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChildren(nid);
        }

        public IEnumerable<IModuleNode> GetChildren(string parentId, string nodeTypeName)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChildren(nid, nodeTypeName);
        }

        public IModuleNode GetChild(string parentId, string nodeTypeName)
        {
            int nid;
            if (!int.TryParse(parentId, out nid) || nid < -1) return null;
            return Manager.GetChild(nid, nodeTypeName);
        }


        public object GetPropertyValue(IModuleNode node, string propertyName)
        {
            return Manager.GetPropertyValue(node, propertyName);
        }

        public IModuleNode GetPropertyAsNode(IModuleNode node, string propertyName)
        {
            return Manager.GetPropertyAsNode(node, propertyName);
        }


        public IEnumerable<KeyValuePair<string, string>> GetProperties(IModuleNode node)
        {
            if (node == null || node.Data == null) return null;
            return Manager.GetProperties(node);
        }
    }
}
